package cn.xf.common.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * LocalDateTime工具类
 * 
 * @author fangxiaofan
 * 
 * @date 2017-07-15
 *
 */
public final class LocalDateTimeUtils {

	/**
	 * 把localDateTime转换为Date
	 * 
	 * @param localDateTime
	 * @return
	 */
	public static Date parseToDate(LocalDateTime localDateTime) {
		
		return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
	}
	
	/**
	 * 把localDate转换为Date
	 * 
	 * @param localDate
	 * @return
	 */
	public static Date parseToDate(LocalDate localDate) {
		
		return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
	}
	
	/**
	 * 把Date转换为localDateTime
	 * 
	 * @param date
	 * @return
	 */
	public static LocalDateTime parseToLocalDateTime(Date date) {
		
		return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
	}
	
	/**
	 * 把Date转换为localDate
	 * 
	 * @param date
	 * @return
	 */
	public static LocalDate parseToLocalDate(Date date) {
		
		return parseToLocalDateTime(date).toLocalDate();
	}
	
	/**
	 * 
	 * 把字符串转换为localDateTime
	 * 
	 * @param text
	 * @param pattern
	 * @return
	 */
	public static LocalDateTime parseToLocalDateTime(String text, String pattern) {
		
		return LocalDateTime.parse(text, DateTimeFormatter.ofPattern(pattern));
	}
	
	/**
	 * 
	 * 把字符串转换为localDate
	 * 
	 * @param text
	 * @param pattern
	 * @return
	 */
	public static LocalDate parseToLocalDate(String text, String pattern) {
		
		return LocalDate.parse(text, DateTimeFormatter.ofPattern(pattern));
	}
	
	/**
	 * 格式化localDateTime
	 * 
	 * @param localDateTime
	 * @param pattern
	 * @return
	 */
	public static String format(LocalDateTime localDateTime, String pattern) {
		
		return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
	}
	
	/**
	 * 格式化LocalDate
	 * 
	 * @param localDate
	 * @param pattern
	 * @return
	 */
	public static String format(LocalDate localDate, String pattern) {
		
		return localDate.format(DateTimeFormatter.ofPattern(pattern));
	}
	
	private LocalDateTimeUtils() {
		
	}
}
